// script.js - 计算机专业学生简历交互功能

// 平滑滚动功能
function smoothScroll() {
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            const targetId = this.getAttribute('href');
            const targetElement = document.querySelector(targetId);
            if (targetElement) {
                window.scrollTo({
                    top: targetElement.offsetTop - 20,
                    behavior: 'smooth'
                });
            }
        });
    });
}

// 技能卡片悬停效果增强
function enhanceSkillHover() {
    const skillItems = document.querySelectorAll('.skill-item');
    skillItems.forEach(item => {
        item.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-3px) scale(1.05)';
            this.style.boxShadow = '0 5px 15px rgba(0,0,0,0.1)';
        });
        item.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0) scale(1)';
            this.style.boxShadow = 'none';
        });
    });
}

// 滚动时显示元素动画
function revealOnScroll() {
    const sections = document.querySelectorAll('.section');
    
    function checkVisibility() {
        sections.forEach(section => {
            const sectionTop = section.getBoundingClientRect().top;
            const windowHeight = window.innerHeight;
            
            if (sectionTop < windowHeight * 0.85) {
                section.style.opacity = '1';
                section.style.transform = 'translateY(0)';
            }
        });
    }
    
    // 初始设置
    sections.forEach(section => {
        section.style.opacity = '0';
        section.style.transform = 'translateY(30px)';
        section.style.transition = 'opacity 0.8s ease, transform 0.8s ease';
    });
    
    // 初始检查
    checkVisibility();
    
    // 滚动时检查
    window.addEventListener('scroll', checkVisibility);
}

// 添加暗黑模式切换（如果需要）
function darkModeToggle() {
    const toggleDarkMode = () => {
        document.body.classList.toggle('dark-mode');
        localStorage.setItem('darkMode', document.body.classList.contains('dark-mode'));
    };
    
    // 检查用户偏好
    if (localStorage.getItem('darkMode') === 'true') {
        document.body.classList.add('dark-mode');
    }
}

// 添加联系方式复制功能
function copyContactInfo() {
    const contactItems = document.querySelectorAll('.contact-item');
    
    contactItems.forEach(item => {
        item.addEventListener('click', function(e) {
            // 对于邮件和电话，我们可以添加复制功能
            if (this.querySelector('.fa-envelope') || this.querySelector('.fa-phone')) {
                e.preventDefault();
                const textToCopy = this.textContent.trim().split(' ').slice(1).join(' ');
                
                navigator.clipboard.writeText(textToCopy).then(() => {
                    const originalText = this.innerHTML;
                    this.innerHTML = '<i class="fas fa-check"></i> 已复制!';
                    this.style.backgroundColor = 'rgba(46, 204, 113, 0.2)';
                    
                    setTimeout(() => {
                        this.innerHTML = originalText;
                        this.style.backgroundColor = '';
                    }, 2000);
                });
            }
        });
    });
}

// 项目经历可折叠功能
function collapsibleProjects() {
    const projectItems = document.querySelectorAll('.project-item');
    
    projectItems.forEach(item => {
        const header = item.querySelector('.project-header');
        const details = item.querySelector('.project-details');
        
        if (header && details) {
            header.addEventListener('click', () => {
                details.classList.toggle('collapsed');
                if (details.classList.contains('collapsed')) {
                    details.style.maxHeight = '0';
                    details.style.opacity = '0';
                } else {
                    details.style.maxHeight = details.scrollHeight + 'px';
                    details.style.opacity = '1';
                }
            });
            
            // 初始设置
            details.style.transition = 'max-height 0.3s ease, opacity 0.3s ease';
            details.style.maxHeight = details.scrollHeight + 'px';
        }
    });
}

// 页面加载完成后执行所有功能
window.addEventListener('DOMContentLoaded', () => {
    smoothScroll();
    enhanceSkillHover();
    revealOnScroll();
    darkModeToggle();
    copyContactInfo();
    collapsibleProjects();
    
    // 打印友好功能提示
    console.log('简历页面已加载完成。祝您求职顺利！');
});

// 窗口调整大小时重新计算项目详情高度
window.addEventListener('resize', () => {
    const projectDetails = document.querySelectorAll('.project-details:not(.collapsed)');
    projectDetails.forEach(detail => {
        detail.style.maxHeight = detail.scrollHeight + 'px';
    });
});

// 添加键盘导航支持
window.addEventListener('keydown', (e) => {
    // 使用方向键在不同部分之间导航
    const sections = document.querySelectorAll('.section');
    let currentIndex = -1;
    
    for (let i = 0; i < sections.length; i++) {
        const rect = sections[i].getBoundingClientRect();
        if (rect.top <= 100 && rect.bottom >= 100) {
            currentIndex = i;
            break;
        }
    }
    
    if (e.key === 'ArrowDown' && currentIndex < sections.length - 1) {
        sections[currentIndex + 1].scrollIntoView({ behavior: 'smooth' });
    } else if (e.key === 'ArrowUp' && currentIndex > 0) {
        sections[currentIndex - 1].scrollIntoView({ behavior: 'smooth' });
    }
});